package com.example.easybuy.service;

import com.alibaba.fastjson.JSONObject;
import com.example.easybuy.dao.UserDao;
import com.example.easybuy.pojo.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService{

    @Resource
    private UserDao userDao;

    private Logger logger=Logger.getLogger(getClass());

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param
     * @return
     */
    @Override
    public String login(String loginName, String password, HttpServletResponse response) {
        logger.debug("UserServiceImpl login loginName: "+loginName+" password : "+password);
        String isUser="";
        try {
            User user=userDao.getLoginName(loginName);
            logger.debug("UserServiceImpl login user: "+user+" password : "+password);
            if(user!=null){
                if(password.equals(user.getPassword())){
                    //  用户存在redis里
                    String users= JSONObject.toJSONString(user);
                    logger.debug("UserServiceImpl login users : "+users);

                    //生命周期：10分钟， TimeUnit.SECONDS主要功能是暂停线程的操作；
                    redisTemplate.opsForValue().set("user",users,60*10,TimeUnit.SECONDS);

                    isUser="loginSuccess";
                }else{
                    isUser="passwordError";
                }
            }else{
                isUser="userNameError";
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("UserServiceImpl login loginName: "+loginName+" password : "+password+" is error ",e);
            isUser="is error";
        }
        return isUser;
    }


    /*注册用户*/
    @Override
    public int addUser(User user) {
        logger.debug("UserServiceImpl-addUser()-...Start...-User:"+user);
        int addUserRow=userDao.addUser(user);
        return addUserRow;
    }

    /*查询登录名是否存在*/
    @Override
    public String getLoginName(String loginName) {
        logger.debug("UserServiceImpl-getLoginName()...Start...loginName:"+loginName);
        User user=userDao.getLoginName(loginName);
        String isExist=null;
        if (user!=null){
            isExist="登录名已存在！";
        }else{
            isExist="";
        }
        return isExist;
    }

    /**
     * 拿redis的用户
     * @return
     */
    @Override
    public User getUser() {
        logger.debug("UserServiceImpl getUser ");
        User userObj=new User();
        //拿到存放在redis里面的登录用户
        Object user=redisTemplate.opsForValue().get("user");
        logger.debug("UserServiceImpl getUser user: "+user);
        if(user!=null){
            String  users=(String)redisTemplate.opsForValue().get("user");
            //把user Json字符串转换为user对象
            userObj = JSONObject.parseObject(users, User.class);
            logger.debug("UserServiceImpl getUser userObj: "+userObj);
            return userObj;
        }else{
            String iserror="500";
            userObj.setLoginStatus(iserror);
            return userObj;
        }

    }
}
